﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
    internal static class CommonExtension
    {
        public static DateTime ToDateTime(this object str)
        {
            try
            {
                return Convert.ToDateTime(str);
            }
            catch (Exception)
            {
                return DateTime.Now;
            }
        }

        public static double ToDouble(this string str, int digits = -1)
        {
            try
            {
                if (digits == -1)
                {
                    return Convert.ToDouble(str);
                }
                else
                {
                    return Math.Round(Convert.ToDouble(str), digits);
                }

            }
            catch (Exception)
            {
                return 0;
            }
        }

        public static DateTime? TryToDateTime(this string str)
        {
            if (str.IsEmpty())
            {
                return null;
            }

            try
            {
                return Convert.ToDateTime(str);
            }
            catch (Exception)
            {
                return DateTime.Now;
            }
        }

        public static int ToInt(this string str)
        {
            try
            {
                return Convert.ToInt32(str);
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public static string GetRoute(this string path)
        {
            string route = path;

            var list = path.Split('/');

            if (list.ToList().Last().IsNumber())
            {
                route = route.Substring(0, route.Length - list.ToList().Last().Length - 1);
            }

            if (route.Contains("?"))
            {
                route = route.Split('?').FirstOrDefault();
            }

            return route;
        }

        public static bool IsNumber(this string str)
        {
            try
            {
                int i = Convert.ToInt32(str);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool IsEmpty(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return true;
            }

            if (string.IsNullOrWhiteSpace(str))
            {
                return true;
            }

            return false;
        }

        public static string CutString(this string str, int count)
        {
            if (str.IsEmpty())
            {
                return string.Empty;
            }

            if (str.Length > count)
            {
                return str.Substring(count);
            }
            else
            {
                return str;
            }
        }

        public static int ToInt(this double dou)
        {
            return Convert.ToInt32(dou);
        }

        public static bool IsInt(this string str)
        {
            int i = 0;

            try
            {
                i = Convert.ToInt32(str);
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

        public static bool IsGrpcRequest(this HttpContext context)
        {
            return !context.Request.ContentType.IsEmpty() && context.Request.ContentType.ToLowerInvariant().Contains("application/grpc");
        }
    }
}
